package com.ddwl.shop.service;

import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.schema.bo.shop.ShopMemberBo;
import com.ddwl.schema.vo.shop.ShopMemberDto;
import com.ddwl.schema.vo.shop.ShopVo;
import com.ddwl.shop.dao.ShopDao;
import com.ddwl.shop.dao.ShopMemberDao;
import com.ddwl.shop.dao.model.Shop;
import com.ddwl.shop.dao.model.ShopMember;
import io.gitee.tooleek.lock.spring.boot.annotation.Lock;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * 门店审核
 * @author wenbin
 * @version 1.0.0.RELEASE
 * @date 2020/3/5 11:00 上午
 */
@Service
public class ShopMemberService {

    @Resource
    private ShopMemberDao shopMemberDao;
    @Resource
    private ShopDao shopDao;
    @Resource
    private ShopService shopService;

    @Transactional(rollbackFor = Throwable.class)
    @Lock(leaseTime=3)
    public void create(ShopMemberBo bo) {
        ParamException.isBlank(bo.getMemberShopId(),"加盟店shopId为空");
        //重复校验
        ShopMember queryShopMember=new ShopMember();
        queryShopMember.setUserId(bo.getUserId());
        queryShopMember.setMemberShopId(bo.getMemberShopId());
        ShopMember member= shopMemberDao.selectOne(queryShopMember);
        if (null==member){
            ShopMember shopMember = BeanConverUtils.convertBean(bo, ShopMember.class);
            //加盟成员店
            Shop shop = shopDao.queryShop(bo.getMemberShopId());
            shopMember.setMemberShopName(shop.getName());
            shopMember.setMemberUserId(shop.getUserId());
            //店主店
            Shop shopByUserId = shopDao.getShopByUserId(bo.getUserId());
            shopMember.setShopName(shopByUserId.getName());
            shopMember.setUserId(shopByUserId.getUserId());
            shopMember.setShopId(shopByUserId.getId());
            shopMemberDao.insert(shopMember);
        }
    }


    public void delete(String id) {
        ParamException.isBlank(id,"id为空");
        shopMemberDao.deleteById(id);
    }

    public List<ShopMemberDto> getShopList(String userId) {
        ParamException.isBlank(userId,"userId为空");
        List<ShopMemberDto> shopVos = shopMemberDao.queryMemberShopList(userId);
        shopVos.stream().forEach(o->shopService.setShopCategoryName(o));
        return shopVos;
    }

    public List<ShopMemberDto> getParentShopList(String userId) {
        ParamException.isBlank(userId,"userId为空");
        List<ShopMemberDto> shopVos = shopMemberDao.queryParentShopList(userId);
        shopVos.stream().forEach(o->shopService.setShopCategoryName(o));
        return shopVos;
    }

    public ShopVo checkMemberShop(String shopId, String memberShopId) {
        ParamException.isBlank(shopId,"门店id为空");
        ParamException.isBlank(memberShopId,"加盟门店id为空");
        ShopMember shopMember = shopMemberDao.checkMemberShop(shopId, memberShopId);
        if (shopMember==null){
            return null;
        }else {
            ShopVo vo = new ShopVo();
            vo.setId(shopMember.getMemberShopId());
            vo.setName(shopMember.getMemberShopName());
            return vo;
        }
    }
}
